module.exports = function($scope, $resource, $modalInstance, companyinfo, toaster){

    $scope.obj = {};
    $scope.flag = false;
    $scope.company_list = [];
    $scope.loadCompany = function(){
        $resource('/api/as/sc/office/getForSubsidyList', {}, {}).save({}, function(res){
            if(res.errcode === 0){
                $scope.company_list = res.data;
                $scope.loadPlace();
            } else {
                toaster.error({title: '', body: '失败'});
            }
        });
    }
    $scope.loadPlace = function(){
        $resource('/api/as/tc/place/getForSubsidyList', {}, {}).save({}, function(res){
            if(res.errcode === 0){
                $scope.company_list = $scope.company_list.concat(res.data);
            } else {
                toaster.error({title: '', body: '失败'});
            }
        });
    }

    if(companyinfo){
        $scope.flag = true;
        $scope.obj.company_name = companyinfo.company_name;
        $scope.obj.company_code = companyinfo.company_code;
        $scope.obj.uplimit_money = companyinfo.uplimit_money / 100;
    } else {
        $scope.loadCompany();
    }

    $scope.ok = function(){
        if($scope.flag){
            if($scope.obj.uplimit_money * 100 < companyinfo.issued_money){
                toaster.warning({title: '', body: '补贴上限不能少于已或得补贴额'});
                return;
            }
        } else {
            if(!$scope.obj.company){
                toaster.warning({title: '', body: '请选择企业'});
                return;
            }
            $scope.obj.company_name = $scope.obj.company.name;
            $scope.obj.company_code = $scope.obj.company.code;
        }
        if(!$scope.obj.company_code){
            toaster.warning({title: '', body: '请填写要添加的企业编号'});
            return;
        }
        if(!$scope.obj.uplimit_money){
            toaster.warning({title: '', body: '请填写补贴上限'});
            return;
        }

        $resource('/api/ac/puc/subsidyCountService/addCompanyLimit', {}, {}).save($scope.obj, function(res){
            if(res.errcode === 0){
                toaster.success({title: '', body: '成功'});
                $modalInstance.close($scope.obj.uplimit_money);
            } else {
                toaster.error({title: '', body: '失败'});
            }
        });
    };

	$scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
	
};